import urllib
from urllib.parse import urlencode
from urllib.request import urlopen

from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSerializer,BadData

import logging
import json
from oauth import constants
from oauth.exceptions import OAuthQQAPIError

logger = logging.getLogger('django')


class OAuthQQ(object):
    """
    QQ认证辅助工具类
    """
    def __init__(self, client_id=None, client_secret=None, redirect_uri=None, state=None):
        self.client_id = client_id if client_id else settings.QQ_CLIENT_ID
        self.redirect_uri = redirect_uri if redirect_uri else settings.QQ_REDIRECT_URI
        # self.state = state if state else settings.QQ_STATE
        self.state = state or settings.QQ_STATE
        self.client_secret = client_secret if client_secret else settings.QQ_CLIENT_SECRET

    def get_qq_login_url(self):
        "获取QQ登录的网址"

        url = 'https://graph.qq.com/oauth2.0/authorize?'
        params = {
            'response_type':'code',
            'client_id':self.client_id,
            'redirect_uri':self.redirect_uri,
            'state':self.state,

        }
        print(params)
        url += urllib.parse.urlencode(params)
        print(url)
        return url

    def get_access_token(self,code):

        url = 'https://graph.qq.com/oauth2.0/token?'

        params = {
            'grant_type': 'authorization_code',
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'code': code,
            'redirect_uri': self.redirect_uri,
        }

        url += urllib.parse.urlencode(params)

        try:
            # 发送请求
            resp = urlopen(url)
            # 读取响应体数据
            resp_data = resp.read().decode()
            # 解析access_token    django自带的urllib  内自带的方法
            # 将查询出来的字符串格式数据转换为Python字典
            resp_dict = urllib.parse.parse_qs(resp_data)
        except Exception as e:
            logger.error('获取access_token异常: %s' % e)
            raise OAuthQQAPIError

        else:
            access_token = resp_dict.get('access_token')
            print(access_token)

            return access_token[0]

    def get_openid(self,access_token):
        url = 'https://graph.qq.com/oauth2.0/me?access_token=' + access_token

        try:
            resp = urlopen(url)

            resp_data = resp.read().decode()
     # callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} )\n;
            # 解析,接触需要的
            resp_data = resp_data[10:-4]
            resp_dict = json.loads(resp_data)
        except Exception as e:
            logger.error('获取openID异常 %s' % e)
            raise OAuthQQAPIError

        else:
            openid = resp_dict.get('openid')
            return openid





    def generate_bind_user_access_token(self,openid):
        serializer = TJWSSerializer(settings.SECRET_KEY,constants.BIND_USER_ACCESS_TOKEN_EXPIRES)

        token = serializer.dumps({'openid':openid})
        return token.decode()

    @staticmethod
    def check_bind_access_token(access_token):
        serializer = TJWSSerializer(settings.SECRET_KEY,constants.BIND_USER_ACCESS_TOKEN_EXPIRES)
        try:
            data = serializer.loads(access_token)
        except BadData:
            return None
        else:
            return data['openid']


